package com.adams.app.model;

import java.util.ArrayList;
import java.util.HashMap;

import android.database.Cursor;

import com.adams.app.dao.PictureDataHelper;
import com.google.gson.Gson;

public class Picture extends BaseType {
	private static final HashMap<Long, Picture> CACHE = new HashMap<Long, Picture>();

	private Long id;
	private Integer sc_id;
	private String name;
	private String urlPic;

	public Long getId() {
		return id;
	}

	public Integer getSc_id() {
		return sc_id;
	}

	public String getName() {
		return name;
	}

	public String getUrlPic() {
		return urlPic;
	}

	private static void addToCache(Picture picture) {
		CACHE.put(picture.getId(), picture);
	}

	private static Picture getFromCache(long id) {
		return CACHE.get(id);
	}

	public static Picture fromJson(String json) {
		return new Gson().fromJson(json, Picture.class);
	}

	public static Picture fromCursor(Cursor cursor) {
		long id = cursor.getLong(cursor
				.getColumnIndex(PictureDataHelper.PictureDBInfo._ID));
		Picture picture = getFromCache(id);
		if (picture != null) {
			return picture;
		}
		picture = new Gson().fromJson(cursor.getString(cursor
				.getColumnIndex(PictureDataHelper.PictureDBInfo.JSON)),
				Picture.class);
		addToCache(picture);
		return picture;
	}

	public static class PictureRequestData {

		private ArrayList<Picture> ScenicJson;

		public ArrayList<Picture> getPictures() {
			return ScenicJson;
		}
	}
}
